/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef OHOS_GRPC_NODE_HTTP2_UTILS_H
#define OHOS_GRPC_NODE_HTTP2_UTILS_H
#include "napi/http2_struct.h"
#include "nlohmann/json_fwd.hpp"
#include "ohos_log.h"
#include <arpa/inet.h>
#include <netdb.h>
#include <string>
#include <sys/socket.h>
#include "securec.h"
#include "nlohmann/json.hpp"
#include "http2_constant.h"

std::vector<std::string> ResolveHostTxt(const std::string &domain)
{
    std::vector<std::string> txtRecords;
    struct addrinfo hints;
    struct addrinfo *res;
    struct addrinfo *p;
    int status;
    memset_s(&hints, sizeof hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    if ((status = getaddrinfo(domain.c_str(), NULL, &hints, &res)) != 0) {
        LOGE("getaddrinfo error:%s", gai_strerror(status));
        return txtRecords;
    }
    for (p = res; p != NULL; p = p->ai_next) {
        char host[NI_MAXHOST];
        if (getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof host, NULL, 0, NI_NAMEREQD) == 0) {
            LOGE("resolveTxt host %s", host);
            txtRecords.push_back(host);
        }
    }
    freeaddrinfo(res);
    return txtRecords;
}

char *FindIPAddr(const std::string &hostname)
{
    struct addrinfo hints;
    struct addrinfo *resultAddr;
    struct addrinfo *model;
    int status;
    memset_s(&hints, sizeof hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    if ((status = getaddrinfo(hostname.c_str(), nullptr, &hints, &resultAddr)) != 0) {
        LOGE("getaddrinfo error:%s", gai_strerror(status));
        return "";
    }
    char *ipv4Temp;
    for (model = resultAddr; model != nullptr; model = model->ai_next) {
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)model->ai_addr;
        char ipstr[INET_ADDRSTRLEN];
        inet_ntop(AF_INET, &(ipv4->sin_addr), ipstr, sizeof ipstr);
        ipv4Temp = ipstr;
    }
    LOGE("FindIPAddr ip %s", ipv4Temp);
    freeaddrinfo(resultAddr);
    return ipv4Temp;
}

int TimeDifference(time_t timestamp1, time_t timestamp2)
{
    return std::abs(timestamp1 - timestamp2);
}

std::string UnorderedMapToJsonString(const std::unordered_map<std::string, std::string> &myMap)
{
    nlohmann::json json = myMap;
    return json.dump();
}

bool CreateArrayBuffer(napi_env env, void *src, size_t srcLen, napi_value *res)
{
    if (src == nullptr || srcLen == 0) {
        return false;
    }
    void *nativePtr = nullptr;
    if (napi_create_arraybuffer(env, srcLen, &nativePtr, res) != napi_ok || nativePtr == nullptr) {
        return false;
    }
    std::copy((uint8_t *)src, (uint8_t *)src + srcLen, (uint8_t *)nativePtr);
    return true;
}

HandleBuffer CheckBufferNum(uint8_t *buffer, int len)
{
    struct HandleBuffer handleBuffer;
    for (int i = 0; i < len; i++) {
        if ((buffer[i] > NUM_31) && (buffer[i] <= NUM_126)) {
            handleBuffer.dirtyDataLen = i;
            handleBuffer.effectiveDataLen = len - i;
            break;
        }
    }
    return handleBuffer;
}

#endif // OHOS_GRPC_NODE_HTTP2_UTILS_H
